Passed
Push — master ( 5f67fe...8bce0b )
by Jesús
02:16
created

displayHelpers.ts ➔ shouldBoxBeActive   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
export interface DisplayState {
2
  indexText: string;
3
  announcement: string;
4
  shouldGetWord: boolean;
5
}
6
7
export function calculateDisplayState(binaryValue: number): DisplayState {
8
  if (binaryValue === 0) {
9
    return {
10
      indexText: '-',
11
      announcement: 'No pattern selected',
12
      shouldGetWord: false,
13
    };
14
  }
15
16
  if (binaryValue > 2048) {
17
    return {
18
      indexText: binaryValue.toString(),
19
      announcement: `Value ${binaryValue} is out of range. Maximum is 2048`,
20
      shouldGetWord: false,
21
    };
22
  }
23
24
  return {
25
    indexText: binaryValue.toString(),
26
    announcement: '', // Will be filled with word info
27
    shouldGetWord: true,
28
  };
29
}
30
31
export function generateWordAnnouncement(word: string, index: number): string {
32
  return `Word selected: ${word}, index ${index}`;
33
}
34
35
export function shouldBoxBeActive(boxState: boolean): boolean {
36
  return boxState;
37
}
38
39
export function shouldBoxBeDisabled(index: number, boxes: boolean[]): boolean {
40
  const is2048Active = boxes[0];
41
  const isCurrentBoxActive = boxes[index];
42
43
  // Box 0 (2048) is disabled if any other box is active and 2048 is not active
44
  if (index === 0) {
45
    const isAnyOtherBoxActive = boxes.slice(1).some(box => box);
46
    return isAnyOtherBoxActive && !is2048Active;
47
  }
48
49
  // Other boxes are disabled if 2048 is active and the current box is not active
50
  return is2048Active && !isCurrentBoxActive;
51
}
52